package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Cppingjia;
import com.xiaomi.service.CppingjiaService;
import com.xiaomi.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cppingjia")
public class CpPingjiaController {
    @Resource
    CppingjiaService cppingjiaService;

    @RequestMapping(value = "/chaxun", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectAll(@RequestParam(value = "yonghuname", required = false) String yonghuname,
                            @RequestParam(value = "zhuangtai", required = false) String zhuangtai,
                            @RequestParam(required = false) Integer page,
                            @RequestParam(required = false) Integer limit) {
        Map<String, Object> pars = new HashMap<>();
        pars.put("yonghuname",yonghuname);
        pars.put("zhuangtai",zhuangtai);
        if (page!=null && limit !=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }
        List<Cppingjia> list =  cppingjiaService.selectAll(pars);
        int count = cppingjiaService.count(pars);
        Result result = new Result(1, "查询成功",count, list);

        return JSON.toJSONString(result);

    }
    @RequestMapping(value = "update",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(Cppingjia cppingjia){
        int i=cppingjiaService.update(cppingjia);
        if (i==1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @RequestMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(Cppingjia cppingjia) {

        int n = cppingjiaService.add(cppingjia);
        if (n == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }

    }
    @RequestMapping(value = "/del", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String del(int id) {

        int n = cppingjiaService.del(id);
        if (n == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }

    }
}
